﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOQLDV;
using System.Data;
using System.Data.OleDb;
using System.Collections;

namespace DAOQLDV
{
    public class DAODiaDiem
    {
        public static List<DTODiaDiem> LayDanhSachDiaDiem()
        {
            List<DTODiaDiem> dsDiaDiem = new List<DTODiaDiem>();
            DataAccess da = new DataAccess();
            try
            {
                da.Open();
                String sql = "Select * From TenDiaDiem";
                OleDbDataReader dr = da.ExecuteQuery(sql);
                while (dr.Read())
                {
                    DTODiaDiem dtoDD = new DTODiaDiem();
                    dtoDD.MaDiaDiem = (int)dr["MaTenDiaDiem"];
                    dtoDD.TenDiaDiem = (String)dr["TenDiaDiem"];
                    dsDiaDiem.Add(dtoDD);
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                da.Close();
            }
            return dsDiaDiem;
        }        
        public static bool ThemDiaDiem(DTODiaDiem dtoDD)
        {
            bool kq = false;
            DataAccess da = new DataAccess();
            try
            {
                da.Open();
                da.BeginTran();
                String sql = "Insert Into TenDiaDiem(TenDiaDiem) Values ('" + dtoDD.TenDiaDiem + "')";
                da.ExecuteNonQuery(sql);
                sql = "Select @@IDENTITY";
                dtoDD.MaDiaDiem = (int)da.ExecuteScalar(sql);
                DAOTuKhoaDiaDiem.ThemTuKhoaDiaDiem(dtoDD, da);
                da.CommitTran();
                kq = true;
            }
            catch (Exception ex)
            {
                da.RollBackTran();
                throw ex;
            }
            finally
            {
                da.Close();
            }
            return kq;
        }
        public static bool CapNhatDiaDiem(DTODiaDiem dtoDD)
        {
            bool kq = false;
            DataAccess da = new DataAccess();
            try
            {
                da.Open();
                String sql = "Update TenDiaDiem Set TenDiaDiem = '" + dtoDD.TenDiaDiem + "' Where MaTenDiaDiem = " + dtoDD.MaDiaDiem;
                da.ExecuteNonQuery(sql);
                kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                da.Close();
            }
            return kq;
        }      
        public static bool XoaDiaDiem(DTODiaDiem dtoDD)
        {
            bool kq = false;
            DataAccess da = new DataAccess();
            try
            {
                da.Open();
                String sql = "Delete From TenDiaDiem Where MaTenDiaDiem = " + dtoDD.MaDiaDiem;
                da.ExecuteNonQuery(sql);
                kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                da.Close();
            }
            return kq;
        }
        public static int LayMaDiaDiem(String tenDiaDiem)
        {
            int kq = 0;
            DataAccess da = new DataAccess();
            try
            {
                da.Open();
                String sql = "Select MaTenDiaDiem From TenDiaDiem Where TenDiaDiem Like '"+tenDiaDiem+"'";
                kq = int.Parse("0" + da.ExecuteScalar(sql));               
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                da.Close();
            }
            return kq;
        }
    }

}
